﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CustomControllerFactory
{
    public class CustomControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            //获取构造函数对象
            var constructor = controllerType.GetConstructors()[0];
            //获取构造函数参数
            var param = constructor.GetParameters();
            //获取构造函数所需的参数
            var paramNames = param.Select(a => {
                var serviceType = (ServiceAttribute)(a.ParameterType.GetCustomAttributes(typeof(ServiceAttribute), true)[0]);
                var t = Type.GetType(serviceType.ServiceName + ", " + serviceType.ServiceNameSpace, true);
                return Activator.CreateInstance(t);
            }).ToArray();
            IController controller = Activator.CreateInstance(controllerType, paramNames) as Controller;
            return controller;
        }
    }
}